<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
	<title>Insert title here</title>
	<link rel="stylesheet" href="https://cdn.bootcss.com/layer/3.0.1/skin/default/layer.min.css">
	<link rel="stylesheet" href="../../../layui/css/layui.css">
</head>
<body>
	<div  style="margin-top: 15px" id="modifysysuser">
		<form class="layui-form" action="">
			
			<div class="layui-form-item">
				<label class="layui-form-label">账号</label>
				<div class="layui-input-inline">
					<input type="text" name="username" value="{{user.username}}" required lay-verify="username"
						placeholder="请输入标题" autocomplete="off" class="layui-input" disabled>
				</div>
				<div class="layui-form-mid layui-word-aux">请输入大写字母，小写字母和数字</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label">姓名</label>
				<div class="layui-input-inline">
					<input type="text" name="name" required lay-verify="name"  value="{{user.name}}"
						placeholder="请输入标题" autocomplete="off" class="layui-input">
				</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label">手机号码</label>
				<div class="layui-input-inline">
					<input type="text" name="phone" required lay-verify="phone"  value="{{user.phone}}"
						placeholder="请输入标题" autocomplete="off" class="layui-input">
				</div>
			</div>
			
			<div class="layui-form-item">
				<label class="layui-form-label">性别</label>
				<div class="layui-input-block">
					<input type="radio" name="sex" value="1" title="男" :checked="user.sex==1"> 
					<input type="radio" name="sex" value="0" title="女" :checked="user.sex==0">
				</div>
			</div>
			
			<div class="layui-form-item">
				<label class="layui-form-label" style="width:90px">启用</label>
				<div class="layui-input-block">
      				<input type="checkbox" name="status" lay-skin="switch" lay-text="启用|禁用" :checked="user.status==0" value="0">
				</div>
			</div>
			
			<div class="layui-form-item"   v-if="is_superadmin==1">
				<label class="layui-form-label" style="width:90px">管理员</label>
				<div class="layui-input-block">
      				<input type="checkbox" name="is_admin" lay-skin="switch" lay-text="是|否" value="0" :checked="user.status==0">
				</div>
			</div>
			
			<div class="layui-form-item">
				<div class="layui-input-block">
					<button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
				</div>
			</div>
		</form>
	</div>

	<!-- jQuery -->
	<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
	<!-- jquery.cookie -->
	<script src="https://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
	<!-- layer -->
	<script src="https://cdn.bootcss.com/layer/3.0.1/layer.min.js"></script>
	<!-- vue -->
	<script src="http://cdn.jsdelivr.net/vue/1.0.7/vue.min.js"></script>
	<script src="../../../layui/layui.js"></script>
	<script src="../../../js/byking.js"></script>
	<script src="../../../js/hostconfig.js"></script>
	<script src="../../../js/byking-verify.js"></script>

	<script>
	
		var deptId = $byking.getUrlParam("guid");
		var userId = $byking.getUrlParam("userId");
		
		var modifysysuser = new Vue({
	        el: "#modifysysuser",
	        data: {
	        	is_superadmin:$byking.getCookieValue("is_superadmin")
	        }
	    });
		
		function getUserInfo() {
			var userId = $byking.getUrlParam("userId");
			var url = $hostconfig.sysAuthHost+"/user/info?guid="+userId;
			$byking.getJSONWithToken(url,function(data){
				modifysysuser.user = data.data;
				setTimeout(function () { 
					var form = layui.form();
					form.render("checkbox");
					form.render("radio");
			    }, 100);
				
			},function(data){});
		}
		
		function initPage(){
			getUserInfo();
		}
		
		initPage();
		
		var submitflag = true;
	
		layui.use('form', function() {
			var form = layui.form();
			
			//验证提交
			form.verify({  
				username: function(value) {  
		          if (!$bykingVerify.checkSpecialChar(value)) {
		        	return '字母开头，允许5-16字节，允许字母数字下划线';
		          }
		        }
	  			,name: function(value) {
	  				if(value.length == 0){  
						return '姓名必须填写';
					}
					if(value.length > 10){  
						return '姓名至多得10个字符';
					}
					if(!$bykingVerify.checkRule1(value)) {
						return '名字只能包括中文、英文、数字包括下划线';
					}
				}
			});

			//监听提交
			form.on('submit(formDemo)', function(data) {
				
				if(!submitflag){
					return false;
				}
				submitflag = false;
				$(".layui-btn:first").attr("disabled","disabled");
				$(".layui-btn").addClass("layui-btn-disabled");
				
				//layer.msg(JSON.stringify(data.field));
				var data = data.field;
				var newData = {
					guid: userId,
					username: data.username,
					dep_guid: deptId,
					name: data.name,
					sex: data.sex,
					phone: data.phone,
					status: 1,
					is_del: 0,
					update_by : $byking.getCookieValue("user_guid")
				};
				if(data.status){
					newData.status = data.status;
				}
				if(data.is_admin){
					newData.is_admin = data.is_admin;
				}
				
				//layer.msg(JSON.stringify(newData));
				$byking.postJSONWithToken($hostconfig.sysAuthHost+"/user/add",newData,function(data){
					layer.msg("修改账户信息成功!");
					var index = parent.layer.getFrameIndex(window.name);
					parent.refrehUserList();
					parent.layer.close(index);
				},function(data){
					submitflag = true;
					$(".layui-btn:first").removeAttr("disabled"); 
					$(".layui-btn").removeClass("layui-btn-disabled");
					layer.msg(data.responseJSON.message, {icon:5});
				});
				return false;
			});
		});
	</script>
</body>
</html>